<?php

App::import('Sanitize');

class GlobalBrandController extends AppController {

    var $name = 'GlobalBrand';
    var $uses = array('Brand', 'Brandsetting');

    function index($id=null, $id2=null) {
        $this->layout = false;
        if (!empty($this->data)):
            if (isset($this->data['brand_type'])):
                if ($this->Brandsetting->save($this->data)) {
                    $this->Session->setFlash(__("Global " . $id . " settings has been updated successfully.", true));
                } else {
                    $this->Session->setFlash(__("Global " . $id . " settings could not be saved. Please, try again.", true));
                }

            else:
                if ($this->Brandsetting->delete($this->data['id'])) {
                    $this->Session->setFlash(__("Global " . $id . " settings has been updated successfully.", true));
                } else {
                    $this->Session->setFlash(__("Global " . $id . " settings could not be saved. Please, try again.", true));
                }
            endif;
        endif;
        $this->data['brand_type'] = $id;
        $this->data['brand_settings'] = $this->Brandsetting->find('first', array('conditions' => array('brand_type' => $id)));
        $this->data['global_brand'] = $this->Brand->globalBrands($id, $id2);
    }

    function deletesettings($id=null) {
        $this->layout = false;
        if ($this->Brandsetting->delete($id)) {
            $return_arr['message'] = "Yes";
        } else {
            $return_arr['message'] = "No";
        }
        echo json_encode($return_arr);
        $this->autoRender = false;
    }

    function success() {
        $this->layout = false;
    }

    function add() {
        $this->layout = false;
        if (!empty($this->data) and $this->data['Brand']['brand_description'] != "Enter global " . $this->data['Brand']['brand_type'] . " here") {
            $this->data = Sanitize::clean($this->data, array('remove_html' => true));
            $this->data['Brand']['brand_description'] = trim($this->data['Brand']['brand_description']);
            $duplicate = $this->Brand->find('count', array('conditions' => array('Brand.brand_description' => $this->data['Brand']['brand_description'], 'Brand.brand_type' => $this->data['Brand']['brand_type'])));
            if ($duplicate > 0) {
                $this->Session->setFlash(__($this->data['Brand']['brand_description'] . " Duplicate Global " . $this->data['Brand']['brand_type'] . ". Please use different", true));
                $this->redirect(array('action' => 'index', $this->data['Brand']['brand_type']));
            } else {
                $this->Brand->create();
                if ($this->Brand->save($this->data)) {
                    $this->Session->setFlash(__("Global " . $this->data['Brand']['brand_type'] . " has been saved successfully.", true));
                    $this->redirect(array('action' => 'index', $this->data['Brand']['brand_type']));
                } else {
                    $this->Session->setFlash(__('The Brand could not be saved. Please, try again.', true));
                }
            }
            $this->data['brand_settings'] = $this->Brandsetting->find('first', array('conditions' => array('brand_type' => $this->data['Brand']['brand_type'])));
            $this->data['global_brand'] = $this->Brand->find('all', array('conditions' => array('Brand.brand_type' => $this->data['Brand']['brand_type']), 'order' => 'Brand.brand_description ASC'));
        }else
            $this->redirect(array('action' => 'index', $this->data['Brand']['brand_type']));
    }

    function edit() {
        $this->layout = false;
        $this->data = Sanitize::clean($this->data, array('remove_html' => true));
        $this->data['Brand']['brand_description'] = trim($this->data['Brand']['brand_description']);
        $return_arr['boxID'] = $this->data['Brand']['id'];
        $return_arr['brandName'] = $this->data['Brand']['brand_description'];
        $duplicate = $this->Brand->find('count', array('conditions' => array('Brand.id !=' => $this->data['Brand']['id'], 'Brand.brand_description' => $this->data['Brand']['brand_description'], 'Brand.brand_type' => $this->data['Brand']['brand_type'])));
        if ($duplicate > 0) {
            $return_arr['message'] = "Duplicate";
        } else {
            if ($this->Brand->save($this->data)) {
                $return_arr['message'] = "Yes";
            } else {
                $return_arr['message'] = "No";
            }
        }
        echo json_encode($return_arr);

        $this->autoRender = false;
    }

    function delete($id = null, $id2=null) {
        $this->layout = false;

        if (!empty($this->data)) {

            if ($this->Brand->delete($id, true)) {
                $b_id = $this->Categorybrand->findById($id, array('brand_id'));
                $this->Categorybrand->query("delete from authentic_categorybrands where id='".$id."'");
                $this->Session->setFlash(__("Global $id2 has been deleted successfully.", true));
                $this->redirect(array('action' => 'index', $id2));
            } else {
                $this->Session->setFlash(__("The global $id2 could not be deleted. Please, try again.", true));
            }
        }
        if ($id && empty($this->data)) {
            $this->data = $this->Brand->read(null, $id);
        }
    }

}

?>